﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravianHelper.app;
using System.Collections;
using System.IO;
using TravianHelper.hero;

namespace TravianHelper.village
{
    public partial class VillageForm : Form
    {
        private Village village;
        private bool addNew;
        public void setVillage(Village village)
        {
            this.village = village;
            textBoxVillageName.Text = village.name;
            textBoxX.Text = village.x.ToString();
            textBoxY.Text = village.y.ToString();
            textBoxVillageId.Text = village.id;
            updateListViewTarget(village.oasis);
            updateListViewTroop(village.troops);
        }
        public VillageForm(bool addNew)
        {
            this.addNew = addNew;
            InitializeComponent();
        }
        private void VillageForm_Load(object sender, System.EventArgs e)
        {
            toolTip1.SetToolTip(this.textBoxVillageId, "This is the id in the link. For example: http://ts4.travian.com/karte.php?newdid=91695 ");
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            village.name = textBoxVillageName.Text;
            village.x = Convert.ToInt32(textBoxX.Text);
            village.y = Convert.ToInt32(textBoxY.Text);
            village.id = textBoxVillageId.Text;
            if (addNew)
            {
                TravianApplicationContext.villages.Add(village);
            }
            TravianApplicationContext.mainForm.reloadComboBoxVillage();
            labelSaveStatus.Text = "Village saved";
        }

        private void buttonSearchAdd_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < TravianApplicationContext.allOasis.Count; i++)
            {
                Zone zone = (Zone)TravianApplicationContext.allOasis[i];
                zone.distance = Math.Sqrt((zone.x - village.x) * (zone.x - village.x) + (zone.y - village.y) * (zone.y - village.y));
                int maxDist = Convert.ToInt32(textBoxMaxDist.Text);
                if (zone.distance <= maxDist && !village.oasis.Contains(zone))
                {
                    village.oasis.Add(zone);
                    zone.defend = int.MaxValue;
                }
            }
            village.oasis.Sort(new ZoneComparer());
            updateListViewTarget(village.oasis);
        }

        private void updateListViewTarget(ArrayList zones)
        {
            listViewTarget.Items.Clear();
            foreach (Zone zone in zones)
            {
                listViewTarget.Items.Add(new ListViewItem(new string[] { zone.x.ToString() + "," + zone.y.ToString(), zone.distance.ToString(), zone.status, zone.defend.ToString() }));
            }
            totalOasis.Text = "Total oasis/farm: " + zones.Count + " Clean: "+village.getNCleanOasis();
        }

        private void updateListViewTroop(ArrayList troops)
        {
            listViewTroop.Items.Clear();
            foreach (Troop troop in troops)
            {
                listViewTroop.Items.Add(new ListViewItem(new string[] { troop.id.ToString(), troop.name, troop.minNumberEachWave.ToString() ,troop.number_each_wave.ToString(), troop.stay.ToString() }));
            }
        }

        private void buttonAddTroop_Click(object sender, EventArgs e)
        {
            try
            {
                string[] tmp = comboBoxTroop.SelectedItem.ToString().Split('_');
                Troop troop = new Troop();
                troop.id = Convert.ToInt32(tmp[0]);
                troop.name = tmp[1];
                troop.attack = Convert.ToInt32(tmp[2]);
                troop.number_each_wave = Convert.ToInt32(textBoxNEachWave.Text);
                troop.minNumberEachWave = Convert.ToInt32(textBoxMinTroop.Text);
                troop.stay = Convert.ToInt32(textBoxTroopStay.Text);
                village.troops.Add(troop);
                updateListViewTroop(village.troops);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please select a troop type");
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(textBoxTargetX.Text);
            int y = Convert.ToInt32(textBoxTargetY.Text);
            Zone zone = new Zone();
            zone.x = x;
            zone.y = y;
            zone.distance = Math.Sqrt((zone.x - village.x) * (zone.x - village.x) + (zone.y - village.y) * (zone.y - village.y));
            village.oasis.Add(zone);
            village.oasis.Sort(new ZoneComparer());
            updateListViewTarget(village.oasis);
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            village.oasis.Clear();
            updateListViewTarget(village.oasis);
        }

        

        private void buttonShuffle_Click(object sender, EventArgs e)
        {
            village.oasis = Util.Shuffle(village.oasis);
            updateListViewTarget(village.oasis);
        }

        private void buttonSort_Click(object sender, EventArgs e)
        {
            village.oasis.Sort(new ZoneComparer());
            updateListViewTarget(village.oasis);
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {                   
                    string filename = openFileDialog1.FileName;
                    StreamReader reader = new StreamReader(filename);
                    string line;
                    while (!reader.EndOfStream)
                    {
                        line = reader.ReadLine();
                        string[] tmp = line.Split(',');
                        Zone zone = new Zone();
                        zone.x = Convert.ToInt32(tmp[0]);
                        zone.y = Convert.ToInt32(tmp[1]);
                        zone.distance = Math.Sqrt((zone.x - village.x) * (zone.x - village.x) + (zone.y - village.y) * (zone.y - village.y));
                        village.oasis.Add(zone);
                    }
                    village.oasis.Sort(new ZoneComparer());
                    updateListViewTarget(village.oasis);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("File format error");
                }
            }
        }

        private void AddListView(string[] items)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.listViewTarget.InvokeRequired)
            {
                AddListViewCallback d = new AddListViewCallback(AddListView);
                this.Invoke(d, new object[] { items });
            }
            else
            {
                listViewTarget.Items.Add(new ListViewItem(items));
            }
        }
        delegate void AddListViewCallback(string[] items);

        private void listViewTroop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewTroop.SelectedItems.Count > 0)
            {
                village.troops.Clear();
                listViewTroop.SelectedItems[0].Remove();
                for (int i = 0; i < listViewTroop.Items.Count; i++)
                {
                    Troop troop = new Troop();
                    troop.id = Convert.ToInt32(listViewTroop.Items[i].SubItems[0].Text);
                    troop.name = listViewTroop.Items[i].SubItems[1].Text;
                    troop.minNumberEachWave = Convert.ToInt32(listViewTroop.Items[i].SubItems[2].Text);
                    troop.number_each_wave = Convert.ToInt32(listViewTroop.Items[i].SubItems[3].Text);
                    troop.stay = Convert.ToInt32(listViewTroop.Items[i].SubItems[4].Text);
                    village.troops.Add(troop);
                }
            }
        }

        private void buttonRemoveVillage_Click(object sender, EventArgs e)
        {
            TravianApplicationContext.villages.Remove(village);
            TravianApplicationContext.mainForm.reloadComboBoxVillage();
            this.Close();
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            string[] tmp = comboBoxTroopType.SelectedItem.ToString().Split('_');
            int id = Convert.ToInt32(tmp[0]);
            int n = Convert.ToInt32(textBoxTroopNumber.Text);
            for (int i = 0; i < village.oasis.Count; i++)
            {
                Zone zone = (Zone)village.oasis[i];
                if (zone.defend <int.MaxValue)
                {
                    bool added = false;
                    while (!added)
                    {
                        added = TravianApplicationContext.adapter.addToFarmList(textBoxLinkAddFarm.Text, zone.x, zone.y, id, n);
                        Console.WriteLine(zone.distance);
                    }
                }
            }
            MessageBox.Show("Farmlist added");
        }

        private void VillageForm_Load_1(object sender, EventArgs e)
        {

        }

        private void buttonCalDef_Click(object sender, EventArgs e)
        {
            village.resetDefend();
            updateListViewTarget(village.oasis);
        }

        private void label16_Click(object sender, EventArgs e)
        {

        }

        private void comboBoxTroopType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label15_Click(object sender, EventArgs e)
        {

        }

        private void textBoxTroopNumber_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBoxLinkAddFarm_TextChanged(object sender, EventArgs e)
        {

        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void totalOasis_Click(object sender, EventArgs e)
        {

        }

        
                
    }
}
